
/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  These are the four essential freedoms with GNU GPL software:
 *  1: freedom to run the program, for any purpose
 *  2: freedom to study how the program works, and change it to make it do what you wish
 *  3: freedom to redistribute copies to help your Free Software girlfriends and friends
 *  4: freedom to distribute copies of your modified versions to your Free Software girlfriends and friends
 *   ,           ,
 *  /             \
 * ((__-^^-,-^^-__))
 * `-_---'  `---_-'
 *  `--|o`   'o|--'
 *      \  `  /
 *       ): :(
 *       :o_o:
 *        "-"
 */


#include "config.h"

#include <stdio.h>

#include "main.h"
#include "splay-tree.h"
#include "mpool.h"
#include "uniqnode.h"

static struct mpool *uniqnode_pool = NULL;

static splay_tree gunode = NULL;

/* The type of a function used to allocate memory for tree root and
   node structures.  The first argument is the number of bytes needed;
   the second is a data pointer the splay tree functions pass through
   to the allocator.  This function must never return zero.  */
static void *uniqnode_splay_tree_allocate_fn (size_t n, void *data);

/* The type of a function used to free memory allocated using the
   corresponding splay_tree_allocate_fn.  The first argument is the
   memory to be freed; the latter is a data pointer the splay tree
   functions pass through to the freer.  */
static void uniqnode_splay_tree_deallocate_fn (void *pointer, void *data);

/* The type of a function used to deallocate any resources associated
   with the key.  */
static void uniqnode_splay_tree_delete_key_fn (splay_tree_key keydata);

/* The type of a function used to deallocate any resources associated
   with the value.  */
static void uniqnode_splay_tree_delete_value_fn (splay_tree_value valuedata);


void
uniqnode_reset (void)
{

  mpool_pool_delete (uniqnode_pool);
  uniqnode_pool = mpool_pool_new ();

  /* indexed on (char *) */
  gunode = splay_tree_new_with_allocator (splay_tree_compare_strings,	/* splay_tree_compare_fn */
					  uniqnode_splay_tree_delete_key_fn,	/* splay_tree_delete_key_fn */
					  uniqnode_splay_tree_delete_value_fn,	/* splay_tree_delete_value_fn */
					  uniqnode_splay_tree_allocate_fn,	/* splay_tree_allocate_fn */
					  uniqnode_splay_tree_deallocate_fn,	/* splay_tree_deallocate_fn */
					  (void *) 0	/* allocate-data */
    );

}

struct unode *
uniqnode (char *name)
{

  splay_tree_node spn = (splay_tree_node) 0;

  if (name == NULL)
    {
      return (NULL);
    }

  spn = splay_tree_lookup ((splay_tree) gunode, (splay_tree_key) name);

  if (spn)
    {
      return ((struct unode *) spn->value);
    }
  else
    {
      return ((struct unode *) 0);
    }
}

void
uniqnode_add (char *name, struct unode *un)
{
  splay_tree_node spn = (splay_tree_node) 0;

  spn = splay_tree_insert (gunode,	/* splay_tree */
			   (splay_tree_key) name, (splay_tree_value) un);

  if (spn)
    {
    }

  return;
}

/* The type of a function used to allocate memory for tree root and
   node structures.  The first argument is the number of bytes needed;
   the second is a data pointer the splay tree functions pass through
   to the allocator.  This function must never return zero.  */
static void *
uniqnode_splay_tree_allocate_fn (size_t n, void *data)
{
  /* pacify gcc */
  if (data)
    {
    }
  return ((void *) mpool_pool_malloc (uniqnode_pool, (size_t) n));
}

/* The type of a function used to free memory allocated using the
   corresponding splay_tree_allocate_fn.  The first argument is the
   memory to be freed; the latter is a data pointer the splay tree
   functions pass through to the freer.  */
static void
uniqnode_splay_tree_deallocate_fn (void *pointer, void *data)
{
  /* pacify gcc */
  if (data)
    {
    }
  if (pointer)
    {
    }
  return;
}

/* The type of a function used to deallocate any resources associated
   with the key.  */
static void
uniqnode_splay_tree_delete_key_fn (splay_tree_key keydata)
{
  /* key is a (char *) deallocated via mpool() system */
  if (keydata)
    {
    }
  return;
}

/* The type of a function used to deallocate any resources associated
   with the value.  */
static void
uniqnode_splay_tree_delete_value_fn (splay_tree_value valuedata)
{
  /* value is unused */
  if (valuedata)
    {
    }
  return;
}


/* End. */
